#pragma once

#include <QSharedPointer>
#include <QDebug>
#include "baseview.h"
#include "basemodel.h"
#include "../observer/observer.h"
#include "../observer/viewmodelsubject.h"

class BaseViewModel : public Observer,public ViewModelSubject
{
public:
    BaseViewModel(BaseViewModelManager* manager=nullptr):ViewModelSubject(manager){}

    /**
     * @brief 传入一个new分配的指针，设置viewmodel对应的view
     * @param viewPtr
     * @return 返回m_view
     */
    virtual QSharedPointer<BaseView> createView(BaseView* viewPtr){
        if(viewPtr == nullptr){
            qDebug() << "the viewPtr is nullptr";
            return nullptr;
        }
        m_view.reset(viewPtr);
        m_view->addObserver(this);
        return m_view;
    }

    /**
     * @brief 传入一个new分配的指针，设置viewmodel对应的model
     * @param modelPtr
     * @return 返回 m_model
     */
    virtual QSharedPointer<BaseModel> createModel(BaseModel* modelPtr){
        if(modelPtr == nullptr){
            qDebug() << "the modelPtr is nullptr";
            return nullptr;
        }
        m_model.reset(modelPtr);
        return m_model;
    }

    /**
     * @brief 设置m_manager为传入的manager
     * @param manager
     */
    virtual void setManager(BaseViewModelManager* manager) {
        if(manager == nullptr){
            qDebug()<<"manager is nullptr";
        }
        m_manager = manager;
    }

    /**
     * @brief 该函数是纯虚函数，不同的实现类实现的update函数不一样
     * @param dataPtr
     */
    virtual void update(QSharedPointer<Data> dataPtr) = 0;

    /**
     * @brief 根据传入的dataPtr设置m_model
     * @param dataPtr
     */
    virtual void setModel(QSharedPointer<Data> dataPtr) {
        if(m_model==nullptr){
            qDebug() << "model don't exist";
            return;
        }
        m_model->setModel(dataPtr);
    }

    /**
     * @brief 获取viewmodel对应的m_model
     * @return m_model
     */
    virtual QSharedPointer<BaseModel> getModel(){
        return m_model;
    }

    /**
     * @brief 显示viewmodel对应的m_view
     */
    virtual void showView(){
        if(m_view == nullptr){
            qDebug() << "view don't exist!";
            return;
        }
        m_view->showView();
    }

    /**
     * @brief 隐藏viewmodel的m_view
     */
    virtual void hideView() {
        if(m_view == nullptr){
            qDebug() << "view don't exist!";
            return;
        }
        m_view->hideView();
    }

    virtual ~BaseViewModel(){}
public:
    QSharedPointer<BaseView> m_view;
    QSharedPointer<BaseModel> m_model;
};
